﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace RPG
{
    public class CGUITrackBar : CGUIObject
    {
        //Private
        private float value;
        private Vector2 sliderSize;
        private Color sliderColor;
        private Color trackBarColor;
        private Color trackBarBorderColor;

        private bool doPull = false;

        //Event Handler
        public TrackBarEventHandler Event_OnLeftPress;
        public TrackBarEventHandler Event_OnRightPress;
        public TrackBarEventHandler Event_OnLeftRelease;
        public TrackBarEventHandler Event_OnRightRelease;
        public TrackBarEventHandler Event_WhileLeftPress;

        public CGUITrackBar(Vector2 _size, Vector2 _position)
        {
            position = _position;
            size = _size;
            sliderSize = new Vector2(4f, size.Y * 2f);
            value = 0f;
            trackBarColor = Color.Blue;
            trackBarBorderColor = Color.Yellow;
            sliderColor = Color.Green;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            Vector2 screenPos = position + parentWindow.windowPosition;
            Vector2 sliderPosMin = new Vector2(screenPos.X + value * size.X - sliderSize.X/2, screenPos.Y - sliderSize.Y/2);
            Vector2 sliderPosMax = new Vector2(screenPos.X + value * size.X + sliderSize.X/2, screenPos.Y + sliderSize.Y); ;
            Primitives2D.FillRect(spriteBatch, screenPos, screenPos + size, trackBarColor);
            Primitives2D.DrawRect(spriteBatch, screenPos, screenPos + size, 0.3f, trackBarBorderColor);
            Primitives2D.FillRect(spriteBatch, sliderPosMin, sliderPosMax, sliderColor);

            if(DrawCircle)
                Primitives2D.DrawCircle(spriteBatch, new Vector2(screenPos.X + value * size.X, screenPos.Y + sliderSize.Y / 4), size.X / 50, size.X / 50, Color.Green, 144);
        }

        public override void Update(GameTime gameTime, MouseState oldMouseState, MouseState newMouseState)
        {
            //Check if the left mouse button was clicked
            if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
            {
                //Checks if Mouse intersects the button
                Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                if (mouseBoundingBox.Intersects(boundingBox))
                {
                    doPull = true;
                    value = (mousePos.X - (position.X + parentWindow.windowPosition.X)) / size.X;
                }
            }

            if (doPull)
            {
                Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                if (mousePos.X - (position.X + parentWindow.windowPosition.X) <= size.X &&
                    mousePos.X >= position.X + parentWindow.windowPosition.X)
                {
                    value = (mousePos.X - (position.X + parentWindow.windowPosition.X)) / size.X;
                }
                else
                {
                    if (mousePos.X < position.X + parentWindow.windowPosition.X)
                        value = 0;
                    else
                        value = 1;
                }
                if(Event_WhileLeftPress != null)
                    Event_WhileLeftPress.Invoke(this);
            }

            if (doPull && newMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed)
            {
                doPull = false;
            }

            //Check if the Left Click event is not null
            if (Event_OnLeftPress != null)
            {
                //Check if the left mouse button was clicked
                if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released)
                {
                    //Checks if Mouse intersects the button
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        //On intersection call OnLeftClick
                        Event_OnLeftPress.Invoke(this);
                    }
                }
            }
            //Check if the Right Click event is not null
            if (Event_OnRightPress != null)
            {
                //Same as above with Right Click
                if (newMouseState.RightButton == ButtonState.Pressed && oldMouseState.RightButton == ButtonState.Released)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnRightPress.Invoke(this);
                    }
                }
            }
            //Check if the Left Release event is not null
            if (Event_OnLeftRelease != null)
            {
                //Check if the Left Mouse Button was released
                if (newMouseState.LeftButton == ButtonState.Released && oldMouseState.LeftButton == ButtonState.Pressed)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnLeftRelease.Invoke(this);
                    }
                }
            }
            //Check if the Right Release event is not null
            if (Event_OnRightRelease != null)
            {
                //Check if the Right Mouse Button was released
                if (newMouseState.RightButton == ButtonState.Released && oldMouseState.RightButton == ButtonState.Pressed)
                {
                    Vector3 mousePos = new Vector3(newMouseState.X, newMouseState.Y, 0);
                    BoundingBox mouseBoundingBox = new BoundingBox(mousePos, mousePos);
                    if (mouseBoundingBox.Intersects(boundingBox))
                    {
                        Event_OnRightRelease.Invoke(this);
                    }
                }
            }
        }

        //Gets
        public float GetValue(){ return value; }

        //Sets
        public bool DrawCircle = true;
        public void SetSliderSize(Vector2 whichSize) { sliderSize = whichSize; }
        public void SetTrackBarSize(Vector2 whichSize) { size = whichSize; }
        public void SetSliderColor(Color whichColor) { sliderColor = whichColor; }
        public void SetTrackBarColor(Color whichColor) { trackBarColor = whichColor; }
        public void SetTrackBarBorderColor(Color whichColor) { trackBarBorderColor = whichColor; }
        public void SetValue(float whichValue) { value = whichValue; }
    }
}
